package edu.depaul.se.courseregistration.jpa.courses;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "COURSE")
@NamedQueries({
    @NamedQuery(name=Course.COURSE_QUERY_ALL,query="select c from Course c"), //Find all Courses
    @NamedQuery(name = Course.COURSE_QUERY_BY_SUBJECT_ID, query = "SELECT c FROM Course c WHERE c.subjectID = :" + Course.SUBJECT_ID_PARAMETER_NAME), //Find by suubjectID.
    @NamedQuery(name = Course.COURSE_QUERY_BY_NAME, query = "SELECT c from Course c WHERE c.courseTitle = :" + Course.COURSE_TITLE_PARAMETER_NAME), //Find course By Title
    @NamedQuery(name = Course.COURSE_QUERY_BY_ID, query = "SELECT c FROM Course c WHERE c.courseNumber = :" + Course.COURSE_ID_PARAMETER_NAME) //Find Course By ID.
})

/**
 *
 * @author lmarco
 */
public class Course implements Serializable {  
        private static final long serialVersionUID = 1L;
    
        public static final String COURSE_QUERY_ALL = "Course.findAll";
        public static final String COURSE_QUERY_BY_SUBJECT_ID = "Course.findBySubject";
        public static final String COURSE_QUERY_BY_NAME = "Course.findByName";
        public static final String COURSE_QUERY_BY_ID = "Course.findById";
        public static final String SUBJECT_ID_PARAMETER_NAME = "subjectID";
        public static final String TERM_PARAMETER_NAME = "term_Title";
        public static final String COURSE_TITLE_PARAMETER_NAME = "courseTitle";
        public static final String COURSE_ID_PARAMETER_NAME = "courseNumber";
    
    @Id
    @Column(name = "COURSE_ID")
    private Integer courseNumber;
    @Column(name = "COURSE_TITLE")
    private String courseTitle;
    @Column(name = "COURSE_DESCRIPTION")
    private String courseBrief;
    @Column(name = "COURSE_PREREQUISITE")
    private String coursePrerequisite;
    
    @Embedded
    private Term term;
    
     
   @OneToMany(cascade = CascadeType.ALL, 
                mappedBy = "courseNumber", 
                fetch= FetchType.EAGER)
    @OrderBy("isOpen DSC")
    private List<Section> courseSectionList;
    
    @JoinColumn(name = "SUBJECT_ID", referencedColumnName = "SUBJECT_ID")
    @ManyToOne(optional = false)  
    private Subject subjectID;
    @XmlTransient
    public Subject getSubjectID() {
        return subjectID;
    }

    public void setSubjectID(Subject subjectID) {
        this.subjectID = subjectID;
    }
    
    @XmlElement(name="sectionList")
    public List<Section> getCourseSectionList() {
        return courseSectionList;
    }

    public void setCourseSectionList(List<Section> courseSectionList) {
        this.courseSectionList = courseSectionList;
    }

 
    public String getCourseBrief() {
        return courseBrief;
    }

    public void setCourseBrief(String courseBrief) {
        this.courseBrief = courseBrief;
    }

    public Integer getCourseNumber() {
        return courseNumber;
    }

    public void setCourseNumber(Integer courseNumber) {
        this.courseNumber = courseNumber;
    }

     public String getCourseTitle() {
        return courseTitle;
    }

    public void setCourseTitle(String courseTitle) {
        this.courseTitle = courseTitle;
    }
    public String getCoursecoursePrerequisite(){
        return coursePrerequisite;
    }
    public void setCoursePrerequisite(String coursePrerequisite){
        this.coursePrerequisite = coursePrerequisite;
    }
    
    
    public Term getTerm() {
        return term;
    }

    public void setTerm(Term term) {
        this.term = term;
    }
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (courseNumber != null ? courseNumber.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Course)) {
            return false;
        }
        Course other = (Course) object;
        if ((this.courseNumber == null && other.courseNumber != null) || (this.courseNumber != null && !this.courseNumber.equals(other.courseNumber))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("\n");
        sb.append("Course[ courseNumber=");
        sb.append(courseNumber);
        sb.append(", courseTitle=");
        sb.append(courseTitle);
        sb.append(", term=[");
        sb.append(term);
        return sb.toString();
    }
}
